<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 */
class PagesController extends AppController {

/**
 * Controller name
 *
 * @var string
 * @access public
 */
	var $name = 'Pages';

/**
 * Default helper
 *
 * @var array
 * @access public
 */
	var $helpers = array('Html','Js');

/**
 * This controller does not use a model
 *
 * @var array
 * @access public
 */
	var $uses = array('Users','Kehoach','Chitieunv','Kqbanhang','Nhanvien','Nvngay','Nvchithi','Donvi','Thuocdv','Thongbao');

	var $components = array('RequestHandler');
/**
 * Displays a view
 *
 * @param mixed What page to display
 * @access public
 */
	function display() {
	   $curUser=$this->Auth->user();
	   $nhanvien= $this->User->query("SELECT nhanviens.tennv, nhanviens.id, nhanviens.diachi, nhanviens.email,nhanviens.sdt, nhanviens.manv,
        chucdanhs.chucdanh,chucdanhs.id, donvis.tendv, donvis.id from nhanviens, users,chucdanhs,donvis 
                            where nhanviens.id=users.nhanvien_id and nhanviens.chucdanh_id=chucdanhs.id
                            and nhanviens.donvi_id=donvis.id
                             and users.id=".$curUser['User']['id']);
    $cd=$nhanvien[0] ['chucdanhs'] ['id'];
    $nv=$nhanvien[0] ['nhanviens'] ['id'];
        //print_r($nv);
        $thongbaos=$this->Thongbao->find('all',array('conditions'=>array('active'=>1,
                                        'or'=>array(array('Thongbao.chucdanh_id'=>$cd),array('Thongbao.nhanvien_id'=>$nv)))));
        $this->set(compact('nhanvien','thongbaos'));
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title_for_layout = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		
		/*
		 *  lay thong bao cac viec chua lam
		 */
		//$donvi_id= $this->Session->read('cur_cha');
		if(substr($this->Session->read('cur_tendv'),0,2)=='TT')
		{
			$donvi_id= $this->Session->read('cur_dv');
		}
		else 
			$donvi_id= $this->Session->read('cur_cha');

		$dv = $this->Thuocdv->find('list',array(
											'conditions'=>array('donvi_id'=>$donvi_id),
											'fields'=>array('donvi','donvi')));
		if (empty($dv))
			$dv = $donvi_id;
		$donvis = $this->Donvi->find('list',array(
										'conditions'=>array('LEFT(Donvi.madv,2)'=>'TT','id'=>$dv),
											'fields'=>array('id','tendv')));
		
		foreach ($donvis as $key =>$value):
			
			$data[$key]['banhang'] = $this->laynvchuabcbh($key);
			$data[$key]['congviec']= $this->laynvchuabccv($key);
			$data[$key]['chithi']= $this->laynvchuabcct($key);
			
		endforeach;
		$this->set(compact('data','donvis'));
		$this->set(compact('page', 'subpage', 'title_for_layout'));
		$this->render(implode('/', $path));
	}
	
	
	function laynvchuabcbh($donvi_id=null)
	{
		$ngay = date('Y-m-d');
		$nhanvien = $this->Chitieunv->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Kehoach.donvi_id'=>$donvi_id,
																'Kehoach.ngaybd <='=>$ngay,
																'Kehoach.ngaykt >='=>$ngay,
																'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>8),
											'fields'=>array('nhanvien_id'),
											'group'=>array('nhanvien_id')));
		$ctnv = array_keys($nhanvien);
		$nvdabc = $this->Kqbanhang->find('list',array(
										'recursive'=>0,'contain'=>'Chitieunv',
										'conditions'=>array('chitieunv_id'=>$ctnv,'ngay'=>$ngay,'dabaocao'=>1),
										'fields'=>array('chitieunv_id','Chitieunv.nhanvien_id'),
										'group'=>array('Chitieunv.nhanvien_id')));
		$nvchuabc = array_diff($nhanvien, $nvdabc);
		$infonv = $this->Nhanvien->find('list',array('conditions'=>array('id'=>$nvchuabc)));
		return $infonv;
	}
	
	function laynvchuabccv($donvi_id=null)
	{
		$ngay = date('Y-m-d');
		$nhanvien = $this->Nvngay->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Kehoach.donvi_id'=>$donvi_id,
																'Kehoach.ngaybd <='=>$ngay,
																'Kehoach.ngaykt >='=>$ngay,
																'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>8),
											'fields'=>array('nhanvien_id'),
											'group'=>array('nhanvien_id')));
		$ngay = date('w');
		$cvnv = array_keys($nhanvien);
		$nvdabc = $this->Nvngay->find('list',array(
										'conditions'=>array('id'=>$cvnv,'ngayth'=>$ngay,'dagiao'=>2),
										'fields'=>array('nhanvien_id'),
										'group'=>array('nhanvien_id')));
		$nvchuabc = array_diff($nhanvien, $nvdabc);
		$infonv = $this->Nvngay->find('list',array(
										'recursive'=>0,'contain'=>array('Nhanvien'),
										'conditions'=>array('nhanvien_id'=>$nvchuabc,'Nvngay.id'=>$cvnv),
										'fields'=>array('Nhanvien.tennv','nhiemvu','Nhanvien.manv')));
		
		return $infonv;
	}
	
	function laynvchuabcct($donvi_id=null)
	{
		$ngay = date('Y-m-d');
		$nhanvien = $this->Nvchithi->find('list',array(
											'recursive'=>0,'contain'=>array('Kehoach'),
											'conditions'=>array('Kehoach.donvi_id'=>$donvi_id,
																'Kehoach.ngaybd <='=>$ngay,
																'Kehoach.ngaykt >='=>$ngay,
																'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <'=>8),
											'fields'=>array('nhanvien_id'),
											'group'=>array('nhanvien_id')));
		$ctnv = array_keys($nhanvien);
		$nvdabc = $this->Nvchithi->find('list',array(
										'conditions'=>array('id'=>$ctnv,'thoihan'=>$ngay,'dagiao'=>2),
										'fields'=>array('nhanvien_id'),
										'group'=>array('nhanvien_id')));
		$nvchuabc = array_diff($nhanvien, $nvdabc);
		$infonv = $this->Nvchithi->find('list',array(
										'recursive'=>0,'contain'=>array('Nhanvien'),
										'conditions'=>array('nhanvien_id'=>$nvchuabc,'Nvchithi.id'=>$ctnv),
										'fields'=>array('Nhanvien.tennv','congviec','Nhanvien.manv')));
		
		return $infonv;
	}

	function capnhattb()
	{
		$this->layout='ajax';
		$donvi_id= $this->Session->read('cur_dv');
		
		$dv = $this->Thuocdv->find('list',array(
											'conditions'=>array('donvi_id'=>$donvi_id),
											'fields'=>array('donvi','donvi')));
		if (empty($dv))
			$dv = $donvi_id;
		$donvis = $this->Donvi->find('list',array(
										'conditions'=>array('LEFT(Donvi.madv,2)'=>'TT','id'=>$dv),
											'fields'=>array('id','tendv')));
		
		foreach ($donvis as $key =>$value):
			
			$data[$key]['banhang'] = $this->laynvchuabcbh($key);
			$data[$key]['congviec']= $this->laynvchuabccv($key);
			
		endforeach;
		$this->set(compact('data','donvis'));
	}
	
}
